
/*
 * ClassName			AssemblyRegistrationHandler
 * Date					8th, June, 2006
 * author 				Lejil
 */

package com.eibus.applicationconnector.dotnet;


import java.util.ArrayList;
import java.util.Iterator;
import com.eibus.test.Util;
import com.eibus.util.system.EIBProperties;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Utility class to register and remove .NET Assemblies.
 */
public class AssemblyRegistrationHandler
{
	private static final ArrayList<String> s_AssembliesList = new ArrayList<String>();
	private static String CORDYS_BIN =  EIBProperties.getInstallDir() +
											  				File.separator + "lib" + File.separator ;
    private static String gac = null;
    
	static
	{
		s_AssembliesList.add("UnitTest");
		s_AssembliesList.add("jTest");
		s_AssembliesList.add("TigerJunit");
		s_AssembliesList.add("vbStyle");
		s_AssembliesList.add("COMJAVALib");
        
        String dotnetHome = System.getenv("DOTNET_SDK_HOME");
        if (dotnetHome != null)
        {
            gac = "\"" + dotnetHome + "\\v2.0\\Bin\\gacutil" + "\"";
        }
        else
        {
            gac = "\"" + "C:\\Program Files\\Microsoft.NET\\SDK\\v2.0\\Bin\\gacutil.exe" + "\"";            
            if(!new File(gac).exists())
            {
                gac = "gacutil";
            }
        }
	}
	
	public void registerAssemblies()
	{
		try
		{  
			String assemblyLocation  = new File(Util.RESOURCE_ROOT + "/dotnetconn/assembly").getAbsolutePath();
			for (Iterator<String> iterator = s_AssembliesList.iterator(); (iterator.hasNext());)
			{
				String  assembly = "\"" + assemblyLocation + File.separator +  iterator.next() +".dll" + "\"";
                 
				//String  command  = execPath + "gacutil -i " + assembly;
				//String  command  = gac + " -i " + assembly;
                String[] command = {gac, "-i", assembly};
				execute(command);
			}
			registerSystemAssmblies(true);
			registerCOMDLL(true);
			copyJarFiles(true);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void removeAssemblies()
	{
		try
		{
			for (Iterator<String>  iterator = s_AssembliesList.iterator(); (iterator.hasNext());)
			{
				String  assembly = iterator.next();
				//String  command  = execPath + "gacutil -u " + assembly;
				//String  command  = gac + " -u " + assembly;
                String[] command = {gac, "-u", assembly};
				execute(command);
			}
			registerSystemAssmblies(false);
			registerCOMDLL(false);
			copyJarFiles(false);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	
	
	private void registerSystemAssmblies(boolean register)
	{
		String[] command;
		if (register)
		{
			 //command  = gac + " -i " + "\"" + CORDYS_BIN + "xmlForDotnet.dll\"";
            command = new String[]{gac, "-i", "\"" + CORDYS_BIN + "xmlForDotnet.dll" + "\""};
		}
		else
		{
			 //command  = gac + " -u " +  "xmlForDotnet";
             command = new String[]{gac, "-u", "xmlForDotnet"};
		}
		execute(command);
	}
	
	
	private void registerCOMDLL(boolean register)
	{
		String[] command;
		String dll = "\"" + CORDYS_BIN + "TigerCOM.dll" + "\"";
		if (register)
		{
			 //command  = "regsvr32 /s " + dll;
             command = new String[]{"regsvr32", "/s", dll};
		}
		else
		{
			 //command  = "regsvr32 /s /u " + dll;
             command = new String[]{"regsvr32", "/s", "/u", dll};
		}
		execute(command);
	}
	
	
	private void copyJarFiles(boolean copy) throws IOException
	{
		String src =  Util.RESOURCE_ROOT +File.separator + "dotnetconn" + File.separator +"jars" + File.separator +"tiger.jar";
		String jarLocation = EIBProperties.getInstallDir() + File.separator +"ext" + File.separator +"tiger.jar";
		if (copy)
		{
			FileChannel source =null;
			FileChannel destination =null;
			try
			{
				 source = new FileInputStream(src).getChannel();
			     destination =  new FileOutputStream(jarLocation).getChannel();
			     destination.transferFrom(source, 0, source.size());
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			finally
			{
				if (source != null)
					source.close();
				if (destination != null)
					destination.close();
			}
		}
		else
		{
			File file = new File(jarLocation);
			if (file.exists())
			{
				file.delete();
			}
		}
	}
	
	private void execute(String[] commandArray)
	{
        for (int i = 0; i < commandArray.length; i++)
        {
            System.out.print(commandArray[i] + " ");
        }
        System.out.println("\n");
          
		try
		{
			Process process = Runtime.getRuntime().exec(commandArray);
            new ConsoleReader(process.getInputStream());
            new ConsoleReader(process.getErrorStream());
			process.waitFor();
			process.destroy();           
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
    
    public static void main(String[] a)
    {
        AssemblyRegistrationHandler obj = new AssemblyRegistrationHandler();
        obj.registerSystemAssmblies(true);
        obj.registerSystemAssmblies(false);
        System.out.println("over");
    }
}

class ConsoleReader extends Thread
{
    BufferedReader m_streamReader;
    
    ConsoleReader(InputStream is)
    {  
        m_streamReader = new BufferedReader(new java.io.InputStreamReader(is));
        this.setDaemon(true);
        this.start();
    }
    
    public void run()
    {  
        try
        {
            while(true)
            {  
                String line = m_streamReader.readLine();
                if(line == null)
                {
                    break;
                }
                
                System.out.println(line);
            }
                
            this.m_streamReader.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }        
    }
}
